package com.lx.chong.repository;

import com.lx.chong.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author: chong
 * @date: 2018/8/23
 */
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    /**
     * 根据账号查询未删除的使用的用户信息
     *
     * @param account
     * @param ifDelete
     * @param useMark
     * @return
     */
    @Query(value = "select * from user where (Account=?1 or Mobile=?1) and IfDelete=?2 and UseMark =?3", nativeQuery = true)
    User findAllByAccountAndIfDeleteAndUseMark(String account, Integer ifDelete, Integer useMark);

    /**
     * 查询部门下的是部门用户
     * @param ifDelete
     * @param useMark
     * @param depId
     * @return
     */
    List<User> findAllByIfDeleteAndUseMarkAndDepartmentId(Integer ifDelete, Integer useMark, Long depId);

    /**
     * 分页查询
     * @param startNum
     * @param total
     * @return
     */
    @Query(value = "SELECT  * FROM USER WHERE IfDelete =0 order by  addDateTime desc LIMIT ?1,?2",nativeQuery = true)
    List<User> getUserListPage(Integer startNum,Integer total);

    /**
     * 获取总数
     * @return
     */
    @Query(value = "SELECT count(1) FROM USER WHERE IfDelete =0 ",nativeQuery = true)
    Long getTotalPage();

    /**
     * 查询未删除的用户
     * @param ifDelete
     * @return
     */
    List<User> findAllByIfDelete(Integer ifDelete);
}
